package com.guapi.controller;

import com.guapi.entity.Discuss;
import com.guapi.entity.Reply;
import com.guapi.entity.User;
import com.guapi.service.DiscussService;
import com.guapi.service.ReplyService;
import com.guapi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Controller
public class ReplyController {
    @Autowired
    private ReplyService replyService;

    @Autowired
    private DiscussService discussService;

    @Autowired
    private UserService userService;

    private List<Discuss> discussList;

    private List<User> userList ;

    private List<Reply> replyList;

    public void init(){
        discussList=discussService.getCache("discussList");
        replyList=replyService.getCache("replyList");
        userList=userService.getCache("userList");
    }
    public void addCache(){
        discussService.setCache("discussList",discussList);
        replyService.setCache("replyList",replyList);
        userService.setCache("userList",userList);
    }
    public void removeCache(){
        discussService.deleteCache("discussList");
        replyService.deleteCache("replyList");
        userService.deleteCache("userList");
    }


    @GetMapping("/reply")
    public String getAllReply(Model model){
        init();
        if(replyList == null || replyList.size() == 0){
            replyList= replyService.getAllReply();
            replyService.setCache("replyList",replyList);
            replyList = replyService.getCache("replyList");

            model.addAttribute("discussList",discussList);
            model.addAttribute("userList",userList);
            model.addAttribute("replyList",replyList);
            model.addAttribute("sum",replyList.size()==0?0:replyList.size());
            return "page/reply";
        }

        model.addAttribute("discussList",discussList);
        model.addAttribute("userList",userList);
        model.addAttribute("replyList",replyList);
        model.addAttribute("sum",replyList.size()==0?0:replyList.size());
        return "page/reply";
    }



    @PostMapping("/reply/insert")
    public String insert(
            @RequestParam("name") String name,
            @RequestParam("content") String content,
            @RequestParam("username") String username){
        //        首先清除缓存
        removeCache();
        Reply reply = new Reply();
        reply.setReplyDiscuss(name);
        reply.setContent(content);
        reply.setUsername(username);
        reply.setDate(new Date());
        replyService.insert(reply);
        replyList = replyService.getAllReply();
        //重新设置缓存
        addCache();
        return "page/reply";
    }

    @DeleteMapping("/reply/delete/{id}")
    public String delete(@PathVariable("id") String id){

        removeCache();
        replyService.delById(id);
        replyList = replyService.getAllReply();
        //重新设置缓存
        addCache();
        return "page/reply";
    }

    @PostMapping("/reply/update")
    public String update(
            @RequestParam("discuss") String name,
            @RequestParam("content") String content,
            @RequestParam("username") String username){

        removeCache();
        Reply reply = new Reply();
        reply.setReplyDiscuss(name);
        reply.setContent(content);
        reply.setUsername(username);
        reply.setDate(new Date());
        replyService.insert(reply);
        replyList = replyService.getAllReply();
        //重新设置缓存
        addCache();
        return "page/reply";
    }
}
